package com.kugong.user;

import com.jfinal.plugin.activerecord.Model;

import java.util.List;

/**
 *
 * mysql> desc tb_user;
 +-------------+--------------+------+-----+---------+----------------+
 | Field       | Type         | Null | Key | Default | Extra          |
 +-------------+--------------+------+-----+---------+----------------+
 | id          | int(11)      | NO   | PRI | NULL    | auto_increment |
 | name        | varchar(255) | NO   | UNI | NULL    |                |
 | passWord    | varchar(255) | NO   |     | NULL    |                |
 | signName    | varchar(50)  | YES  |     | NULL    |                |
 | points      | int(11)      | YES  |     | NULL    |                |
 | create_time | datetime     | YES  |     | NULL    |                |
 | level       | int(10)      | NO   |     | 0       |                |
 +-------------+--------------+------+-----+---------+----------------+
 * Created by Administrator on 2015/8/5.
 */


public class User extends Model<User> {

    public final static User dao = new User();

    /**
     * 根据用户名查找第一个用户信息
     * @param name
     * @return Model<User>
     */
    public User qryUserByName(String name){
        return findFirst("select * from tb_user where name = ?", name);
    }

    /**
     * 查询分值最高的10名选手
     * @return
     */
    public List<User> qryBestPlayer(){
        List<User> list = find("select * from tb_user ORDER BY score DESC limit 10");
        return list;
    }

    /**
     * 查询与微信绑定的账号信息
     * 要求微信登陆事件在300s内
     * 用户名密码没有变化
     * @param open_id
     * @return
     */
    public User qryWeixinUser(String open_id){
        User user = findFirst("SELECT tu.* FROM tb_weixin twx,tb_user tu where tu.name=twx.name and tu.passWord=twx.passWord and twx.open_id=? and TIMESTAMPDIFF(SECOND,twx.modify_time,NOW())<300",open_id);
        return user;
    }
}

